package first;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import priv.tuyou.bo.Page;
import priv.tuyou.bo.SqlParam.Condition;
import priv.tuyou.bo.SqlParamGroup;
import priv.tuyou.dao.DaoImpl;
import priv.tuyou.dao.IDao;

public class MyTest {
	
	protected static ClassPathXmlApplicationContext context;
	
	@BeforeClass
	public static void setup() {
		
		context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	}
	
	
	@AfterClass
	public static void close(){
		
		context.close();
	}
	
	@Test
	public void testGet(){
		
		IDao dao = context.getBean(DaoImpl.class);
		User user = dao.get(User.class, 4l);
		System.err.println(user);
	}
	
	@Test
	public void testGetField(){
		
		IDao dao = context.getBean(DaoImpl.class);
		Object user = dao.getField(User.class, 4l, "name");
		System.err.println(user);
	}
	
	@Test
	public void testGetField2(){
		
		IDao dao = context.getBean(DaoImpl.class);
		Object user = dao.getField(User.class, 4l, "name", "age");
		System.err.println(user);
	}
	
	@Test
	public void testDelete(){
		
		IDao dao = context.getBean(DaoImpl.class);
		int effect = dao.delete(User.class, 1l);
		System.err.println(effect);
	}
	
	@Test
	public void testRealDelete(){
		
		IDao dao = context.getBean(DaoImpl.class);
		int effect = dao.realDelete(User.class, 1l);
		System.err.println(effect);
	}
	
	@Test
	public void testSave(){
		
		User user = new User();
		user.setName("涂有");
		user.setAge(25);
		
		IDao dao = context.getBean(DaoImpl.class);
		int effect = dao.save(user);
		System.err.println(effect);
	}
	
	@Test
	public void testUpdate(){
		
		IDao dao = context.getBean(DaoImpl.class);
		User user = dao.get(User.class, 5l);
		user.setName("中文名字");
		int effect = dao.update(user);
		System.err.println(effect);
	}
	
	@Test
	public void testUpdateApart(){
		
		IDao dao = context.getBean(DaoImpl.class);
		User user = dao.get(User.class, 5l);
		user.setName("中文名字");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("age", 26);
		params.put("name", "杨瑞");
		
		int effect = dao.update(User.class, user.getId(), params);
		System.err.println(effect);
	}
	
	@Test
	public void testQueryAll(){
		
		IDao dao = context.getBean(DaoImpl.class);
		List<User> users = dao.queryAll(User.class);
		System.err.println(users);
	}
	
	@Test
	public void testQueryAllIgnoreDeleted(){
		
		IDao dao = context.getBean(DaoImpl.class);
		List<User> users = dao.queryAllIgnoreDeleted(User.class);
		System.err.println(users);
	}
	
	@Test
	public void testQuery(){
		
		SqlParamGroup group = new SqlParamGroup("name", Condition.eq, "涂有");
		group.or("name", Condition.eq, "杨瑞");
		
		IDao dao = context.getBean(DaoImpl.class);
		List<User> users = dao.query(User.class, group);
		System.err.println(users);
	}
	
	@Test
	public void testQueryIgnoreDeleted(){
		
		SqlParamGroup group = new SqlParamGroup("name", Condition.eq, "涂有");
		group.or("name", Condition.eq, "杨瑞");
		
		IDao dao = context.getBean(DaoImpl.class);
		List<User> users = dao.queryIgnoreDeleted(User.class, group);
		System.err.println(users);
	}
	
	@Test
	public void testQueryField(){
		
		SqlParamGroup group = new SqlParamGroup("name", Condition.eq, "涂有");
		group.or("name", Condition.eq, "杨瑞");
		
		
		IDao dao = context.getBean(DaoImpl.class);
		List<Map<String, Object>> users = (List<Map<String, Object>>)dao.query(User.class, group, "name");
		System.err.println(users);
	}
	
	@Test
	public void testQueryFieldIgnoreDeleted(){
		
		SqlParamGroup group = new SqlParamGroup("name", Condition.eq, "涂有");
		group.or("name", Condition.eq, "杨瑞");
		
		IDao dao = context.getBean(DaoImpl.class);
		List<Map<String, Object>> users = (List<Map<String, Object>>)dao.queryIgnoreDeleted(User.class, group, "name");
		System.err.println(users);
	}
	
	@Test
	public void testCount(){
		
		IDao dao = context.getBean(DaoImpl.class);
		long count = dao.count(User.class);
		System.err.println(count);
	}
	
	@Test
	public void testCountIgnoreDeleted(){
		
		IDao dao = context.getBean(DaoImpl.class);
		long count = dao.countIgnoreDeleted(User.class);
		System.err.println(count);
	}
	
	@Test
	public void testByConditionCount(){
		
		SqlParamGroup group = new SqlParamGroup("name", Condition.eq, "涂有");
		
		IDao dao = context.getBean(DaoImpl.class);
		long count = dao.count(User.class, group);
		System.err.println(count);
	}
	
	@Test
	public void testByConditionCountIgnoreDeleted(){
		
		SqlParamGroup group = new SqlParamGroup("name", Condition.eq, "涂有");
		
		IDao dao = context.getBean(DaoImpl.class);
		long count = dao.countIgnoreDeleted(User.class, group);
		System.err.println(count);
	}
	
	@Test
	public void testPageQuery(){
		
		Page<User> page = new Page<User>(2, 2);
		
		IDao dao = context.getBean(DaoImpl.class);
		dao.pageQuery(User.class, page);
		System.err.println(page.getTotalCount());
		System.err.println(page.getData());
	}
	
	@Test
	public void testPageQueryIgnoreDeleted(){
		
		Page<User> page = new Page<User>(2, 2);
		
		IDao dao = context.getBean(DaoImpl.class);
		dao.pageQueryIgnoreDeleted(User.class, page);
		System.err.println(page.getTotalCount());
		System.err.println(page.getData());
	}
}
